<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="../jquery-3.1.1.js"></script>
    <title>弹窗</title>
    <style>
        body{
            text-align: center;
            margin: 0;
            width: 100vw;
            height: 100vh;
            background-color: gray;
            font-size: 16px;
        }
        .show{
            width: 8vw;
            margin-top: 5vw;
            /*display: none;*/
        }
        .dialog{
            width: 30vw;
            height: 14vw;
            background-color: white;
            position: absolute;
            top: 50%;
            left: 50%;
            margin-top: -7vw;
            margin-left: -15vw;
            display: none;
        }
        .close{
            font-size: 1.5rem;
            float: right;
            position: relative;
            right: 20px;
            cursor: pointer;
        }
        h4{
            margin: 0;
            padding: 0;
            height: 3vw;
            line-height: 3vw;
            background-color: lightgray;
            cursor: move;
        }
        .content{
            width: 18vw;
            margin: 1.2vw auto;
        }
        .login{
            width: 18vw;
            height: 2vw;
            margin: 0 auto;
            line-height: 1.8vw;
            background-color: lightgray;
        }
    </style>
</head>
<body>
    <input type="button" value="点击显示" class="show">
    <div class="dialog">
        <h4>用户登录<span class="close">×</span></h4>
        <div class="content">
            <label for="">账号：</label>
            <input type="text">
        </div>
        <div class="content">
            <label for="">密码：</label>
            <input type="text">
        </div>
        <div class="login">登录</div>
    </div>
</body>
</html>
<script>
    var mouseX,mouseY;
    var dialogX,dialogY;
    var isDrag = false;//表示不可拖动
    $('.show').click(function(){
        $('.dialog').show();
        //鼠标按下 获取位置
        $('h4').mousedown(function(ev){
            ev = ev || window.event;
            mouseX = ev.pageX - $('.dialog').width()/2;
            mouseY = ev.pageY - $('.dialog').height()/2;
            dialogX = $('.dialog').offset().left;
            dialogY = $('.dialog').offset().top;
            isDrag = true;
        });
        $('.dialog').mouseup(function(){
            isDrag = false;
        });
        
    });

    //鼠标移动更新位置窗口
    $(document).mousemove(function(ev){
        var ev = ev || window.event;
        var x = ev.pageX;
        var y = ev.pageY;

        if(isDrag){
            var moveX = dialogX + x - mouseX;
            var moveY = dialogY + y - mouseY;

            var pageW = $(window).width();
            var pageH = $(window).height();
            var dialogW = $('.dialog').width();
            var dialogH = $('.dialog').height();
            var maxX = pageW - dialogW/2;       //X轴可拖动最大值
            var maxY = pageH - dialogH/2;       //Y轴可拖动最大值
            moveX = Math.min(Math.max(dialogW/2,moveX),maxX);     //X轴可拖动范围
            moveY = Math.min(Math.max(dialogH/2,moveY),maxY);     //Y轴可拖动范围
                    //重新设置对话框的left、top
            $('.dialog').css({"left":moveX + 'px',"top":moveY + 'px'});
        }
    });
    $('.close').click(function(){
        $('.dialog').hide();
    });
    if(document.attachEvent){
                $('.dialog').attachEvent('onselectstart', function() {
                  return false;
                });
            }
</script>